package com.activemq;

import lombok.extern.slf4j.Slf4j;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.boot.autoconfigure.jms.JmsProperties;

import javax.jms.*;

@Slf4j
public class TestConsumer {
    public static void main(String[] args) throws Exception {
        ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(MqHelloWorld.URL);
        Connection connection = factory.createConnection();
        connection.start();
        Session session = connection.createSession(false, JmsProperties.AcknowledgeMode.AUTO.getMode());
        Queue queue = session.createQueue(MqHelloWorld.QUEUE);
        MessageConsumer consumer = session.createConsumer(queue);
        while (true) {
            //过时不候
            Message receive = consumer.receive(4000L);
            //一直等候
//            Message receive = consumer.receive();
            if (receive instanceof TextMessage) {
                String text = ((TextMessage) receive).getText();
                log.error(text);
            } else {
                break;
            }
        }
        consumer.close();
        session.close();
        connection.close();
    }
}